﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using RollCallWebsite.localhost;
using System.IO;
using RKLib.ExportData;
using System.Text;


namespace RollCallWebsite
{
    public partial class _Default : System.Web.UI.Page
    {
        Service ser = new Service();
        DataTable data;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime fr = DateTime.Now;
            DateTime to = DateTime.Now;
            try
            {
                fr = DateTime.Parse(TextBox1.Text.Trim());
            }
            catch
            {
                fr = DateTime.Now; ;
            }
            try
            {
                to = DateTime.Parse(TextBox2.Text.Trim());
            }
            catch
            {
                to = DateTime.Now;
            }
            String username = TextBox3.Text.Trim();
            switch (DropDownList1.SelectedIndex)
            {
                case 0:
                    {
                        GridView2.DataSource = ser.GetAllMACaddressOfUserInTime(username, fr, to);
                        break;
                    }
                case 1:
                    {
                        data = ser.GetAllUserInfoOverviewInTime(fr, to);
                        GridView2.DataSource =data;
                        break;
                    }
                case 2:
                    {
                        //data = ser.getAllUserOfMACaddressInTime(username, fr, to);
                        GridView2.DataSource = ser.GetAllUserOfMACaddressInTime(username, fr, to);
                        break;
                    }
                case 3:
                    {
                        data = ser.GetAllUsersAtRTD();
                        GridView2.DataSource = data;
                        break;
                    }
                case 4:
                    {
                        data = ser.GetAllUsersInfoDetailInDay(fr);
                        data.TableName = "GetAllUsersInfoDetailInDay";
                        GridView2.DataSource = data;
                        break;
                    }
                case 5:
                    {
                        data = ser.GetAllUsersInfoInThisWeek();
                        GridView2.DataSource = data;
                        break;
                    }
                case 6:
                    {
                        data = ser.GetUserInfoDetailInDay(username, fr);
                        GridView2.DataSource = data;
                        break;
                    }
                case 7:
                    {
                        data = ser.GetUserInfoDetailInTime(username, fr, to);
                        GridView2.DataSource =data;
                        break;
                    }
                case 8:
                    {
                        data = ser.GetUserInfoInThisWeek(username);
                        GridView2.DataSource =data;
                        break;
                    }
                case 9:
                    {
                        data = ser.GetUserInfoOverviewInTime(username, fr, to);
                        GridView2.DataSource = data;
                        break;
                    }
                case 10:
                    {
                        data = ser.GetAllUsersInfoInTime(fr, to);
                        GridView2.DataSource = data;
                        //Label3.Text = ser.UpdateUserInfoInDay(username, fr, username, username, username).ToString();
                        break;
                    }
            }
            GridView2.DataBind();
            Label2.Text = DropDownList1.SelectedItem.ToString();
            Session["data"] = data;
            Session["name"] = Label2.Text;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (GridView2.Rows.Count > 0)
            {
                ExportGridView(GridView2);
            }
            else
            {
                Label2.Text = "Data is null";
            }
        }
        

        private void ExportGridView(GridView gv)

        {   

            string attachment = "attachment; filename=\""+((String)Session["name"]).Trim()+"("+DateTime.Now.ToShortDateString()+").xls\"";

            Response.ClearContent();

            Response.AddHeader("content-disposition", attachment);

            Response.ContentType = "application/ms-excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);

            gv.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();
    
      }

 

    public override void VerifyRenderingInServerForm(Control control)

    {

    }

    }
}
